package com.example.controller;

import com.example.pojo.Result;
import com.example.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
//        把文件的内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
//        保证文件的唯一性，防止文件覆盖
//     originalFilename.substring(originalFilename.lastIndexOf(".") 提取原始文件名（originalFilename）的扩展名（如 .jpg、.pdf）
        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("E:\\BaiduNetdiskDownload\\springboot3+vue3\\files\\"+filename));
//       file.getInputStream() 把我图片以流的形式存储到oss阿里云当中
        String url=AliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
